package com.blb.friendsO.servlet;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;

/**
 * 基础的Servlet
 */
public class BaseServlet extends HttpServlet {

    private HttpServletRequest req;
    private HttpServletResponse resp;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.req = req;
        this.resp =resp;
        String uri = req.getRequestURI();
        String methodStr = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = getClass().getDeclaredMethod(methodStr, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            System.out.println("没找到方法");
            e.printStackTrace();
        } catch (IllegalAccessException | InvocationTargetException e) {
            System.out.println("调用方法失败");
            e.printStackTrace();
        }
    }

    /**
     * 获取当前第几页
     * @return
     */
    protected int getPage(){
        String page = req.getParameter("page");
        if(page==null){
            page = "1";
        }
        return Integer.parseInt(page);
    }

    /**
     * 获取每页多少条
     * @return
     */
    protected int getSize(){
        String size = req.getParameter("size");
        if(size==null){
            size = "10";
        }
        return Integer.parseInt(size);
    }

    /**
     * 将表单参数获取并封装到实体类中
     * @param tClass
     * @param <T>
     * @return
     */
    protected <T> T getEntity(Class<T> tClass) throws IllegalAccessException, InstantiationException {
        T t = tClass.newInstance();

        //获取实体类中所有的属性   id  name  pwd
        Field[] fields = tClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            //获取实体类属性的名称    name
            String name = field.getName();
            String value = req.getParameter(name);
            if(value!=null){
                if (field.getType() == int.class){
                    field.set(t, Integer.parseInt(value));
                }else if (field.getType() == long.class){
                    field.set(t, Long.parseLong(value));
                }else if (field.getType() == BigDecimal.class){
                    field.set(t,new BigDecimal(value));
                }else {
                    field.set(t,value);
                }
            }
        }
        return t;
    }

    /**
     * 转发
     * @param url
     * @throws ServletException
     * @throws IOException
     */
    protected void forward(String url) throws ServletException, IOException {
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 回写json数据（Ajax专用）
     * @param body
     * @throws IOException
     */
    protected void writeJson(String body) throws IOException {
        resp.setContentType("application/json; charset=utf8");
        resp.getWriter().write(body);
    }
}
